<?php

namespace App\Admin\Controllers\Dedusting;

use App\Models\DedustingProject;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Illuminate\Http\Request;

class ProjectController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('项目列表')
            ->description('')
            ->body($this->grid());
    }

    protected function grid()
    {
        return new Grid(new DedustingProject(), function (Grid $grid) {
            $grid->column('name', '名称');
            $grid->column('location', '地点');
            $grid->column('created_at', '创建时间');
            $grid->column('updated_at', '修改时间');
        });
    }

    public function update($id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content){
        return $content
            ->title('新建项目')
            ->description('创建')
            ->body($this->form());
    }

    public function form(){
        return Form::make(new DedustingProject(), function (Form $form) {
            $form->text('name', '名称')->required();
            $form->text('location', '地点')->required();
            $form->hidden('user_id')->value(Admin::user()->id);

            $form->saving(function (Form $form) {
                $form->user_id = Admin::user()->id;
            });
        });
    }

    public function edit($id,Content $content){
        return $content
            ->title('项目编辑')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }
}